const express = require('express');
const operation = require('../controller/operation')
const router = express.Router();
// 获取球馆信息
router.get('/getInfo',(req,res)=>{
    if(req.query.sid){
    let sql = `select * from stadium where sid=${req.query.sid}`
    operation.execute(sql,null).then(data=>{
        res.json({
            status:200,
            data,
            msg:'获取球馆信息成功'
        })
    },err=>{
        res.json({
            status:400,
            msg:'获取球馆信息失败'
        })
    })    
    }else
    operation.selectAll('stadium').then(data=>{
        res.json({
            status:200,
            data,
            msg:'获取球馆信息成功'
        })
    },err=>{
        res.json({
            status:400,
            msg:'获取球馆信息失败'
        })
    })
})
// 获取球馆信息（分页）
router.get('/pagination',(req,res)=>{
    let pagination=req.query;
    console.log(pagination);
    let currentPage = pagination.currentPage||1;
    let pageSize = pagination.pageSize||6;
    let offset = (currentPage-1)*pageSize;
    let sql =`select * from stadium limit ${pageSize} offset ${offset}`;
    Promise.all([operation.getTotal('stadium'),operation.execute(sql,null)]).then(values=>{
        res.json({
            status:200,
            data:{ pagination:{currentPage,pageSize,total:values[0][0].total},data:values[1]},
            msg:'获取球馆信息成功'  
        })
    }).catch(err=>{
        res.json({
            status:400,
            msg:'失败'  
        })
    })
})
// 添加球馆
router.post('/add',(req,res)=>{
    let data = {sName,sPhone,sAddress,picUrl,intro} = req.body;
    operation.addStadium(data).then(data=>{
        res.json({
            status:201,
            data,
            msg:'添加球馆信息成功'
        })
    },err=>{
        res.json({
            status:400,
            msg:'添加球馆失败'
        })
    }).catch(err=>{
        res.json({
            status:400,
            msg:'添加球馆失败'
        })
    })
})
// 更新球馆信息
router.put('/update',(req,res)=>{
    let data = {sName,sPhone,sAddress,sid,picUrl,intro} = req.body;
    operation.updateStadium(data).then(result=>{
        res.json({
            status:200,
            result,
            msg:'更新成功'
        })
    },err=>{
        res.json({
            status:400,
            msg:'删除失败'
        })
    }).catch(err=>{
        res.json({
            status:400,
            msg:'添加球馆失败'
        })
    })
})
// 删除球馆
router.delete('/remove',(req,res)=>{
    let {query} = req.query;
    operation.remove(query,'sid','stadium').then(result=>{
        res.json({
            status:200,
            result,
            msg:'删除成功'
        })
    },err=>{
        res.json({
            status:400,
            msg:'删除失败'
        })
    })
})
// 球馆模糊查询
router.get('/search',(req,res)=>{
    let pload = req.query;
    let query = pload.query;
    let currentPage = pload.currentPage||1;
    let pageSize = pload.pageSize||6;
    let offset = (currentPage-1)*pageSize;
    let sql =`SELECT * FROM stadium WHERE sname LIKE '%${query}%' 
    or phone LIKE '%${query}%'
    or address LIKE '%${query}%'
    or sid LIKE '%${query}%'`;
    let pageString=`limit ${pageSize} offset ${offset}`;
    Promise.all([operation.execute(sql,null),operation.execute(sql+pageString,null)]).then(values=>{
        res.json({
            status:200,
            data:{ pagination:{currentPage,pageSize,total:values[0].length},data:values[1]},
            msg:'获取球馆信息成功'  
        })
    }).catch(err=>{
        res.json({
            status:400,
            msg:'没有找到相关球馆！'  
        })
    })

})

module.exports = router;